package com.joy.web.common.controller;


import com.joy.common.model.ApiResponse;
import com.joy.web.base.controller.BaseController;
import com.joy.web.common.service.MailService;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文件信息表 前端控制器
 * </p>
 *
 * @author yjzhao
 * @since 2019-09-20
 */
@Slf4j
@Controller
@RequestMapping("/mail")
public class MailController extends BaseController {


    @Resource
    MailService mailService;

    @Autowired
    private freemarker.template.Configuration templateConfiguration;

    @PostMapping(value = "/sendSimpleMail", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public ApiResponse sendSimpleMail(String toAddr, String subject, String content) {
        mailService.sendSimpleMail(toAddr, subject, content);
        return null;
    }

    /**
     * @Description 发送html邮件
     * @return
     * @Author yjzhao
     * @Date 2019/9/24 8:44
     **/
    @RequestMapping(value = "/sendHtmlMail")
    @ResponseBody
    public void sendHtmlMail(String toAddr, String subject, Object params) throws Exception {

        //TODO 模板
        Template htmlTemplate = templateConfiguration.getTemplate("");
        Map<String,Object> model = new HashMap<>();
        model.put("params",params);
        String text = FreeMarkerTemplateUtils.processTemplateIntoString(htmlTemplate,model);
        mailService.sendHtmlMail("", "", text);
    }


}
